package com.ldatum.crm.action.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.ldatum.crm.domain.Announcement;
import com.ldatum.crm.domain.Level;
import com.ldatum.crm.service.AnnouncementService;
import com.ldatum.crm.serviceImpl.AnnouncementServiceImpl;
@ParentPackage("admin")
public class AnnouncementAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private AnnouncementService announcementService = new AnnouncementServiceImpl();
	
	private Announcement announcement;
	
	private int id;
	private String title;
	private String content;
	private String time;
	private String author;
	
	private String ids; //接收前台传递过来的id

	
	
	private List<Level> levelList = new ArrayList<Level>();
	private List<Announcement> announcementList = new ArrayList<Announcement>(); 
	
	/**
	 * 保存公告信息
	 * @throws IOException
	 * @throws SQLException 
	 */
	public void save() throws IOException, SQLException{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("type/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out;
		out = response.getWriter();
		JSONObject json = new JSONObject();
		int result = announcementService.save(announcement);
		if(result==1){
			json.accumulate("success", true);
		}else{
			json.accumulate("success", false);
		}
		out.println(json.toString());
		out.flush();
		out.close();
		
	}
	
	/**
	 * 
	 * 描述 : <公告列表>.
	 * @return 
	 */
	public String announcementList(){
		try {
			System.out.println("sss");
			announcementList = announcementService.list();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "listAnnouncement";
	}
	
	/**
	 * 
	 * 描述 : <会员信息>.
	 * @return
	 */
	public void findById() throws Exception{
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			PrintWriter out;
			out = response.getWriter();
			JSONObject json = new JSONObject();
			announcement = announcementService.findById(id);
			if(announcement!=null){
				json.accumulate("success", true);
				json.accumulate("announcement", announcement);
			}
			out.println(json.toString());
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public Announcement getAnnouncement() {
		return announcement;
	}

	public void setAnnouncement(Announcement announcement) {
		this.announcement = announcement;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public List<Level> getLevelList() {
		return levelList;
	}

	public void setLevelList(List<Level> levelList) {
		this.levelList = levelList;
	}

	public List<Announcement> getAnnouncementList() {
		return announcementList;
	}

	public void setAnnouncementList(List<Announcement> announcementList) {
		this.announcementList = announcementList;
	}

	
}
